package org.crumbleworks.crumbchat.client.gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.crumbleworks.crumbchat.client.controller.ClientControllerInterface;

public class LoginPanel extends JPanel implements ActionListener {
    private static final long serialVersionUID = 1L;

    private ClientControllerInterface controller;

    private JLabel usernameFieldLabel, hostFieldLabel;
    private JTextField usernameField, hostField;
    private JButton connectButton;

    public LoginPanel(ClientControllerInterface guiController) {
        this.controller = guiController;

        setLayout(new GridBagLayout());

        initComponents();
        placeComponents();

        setVisible(true);
    }

    private void initComponents() {
        usernameFieldLabel = new JLabel("Username:");
        hostFieldLabel = new JLabel("Host:");

        usernameField = new JTextField(20);
        hostField = new JTextField(10);

        connectButton = new JButton("connect");
        connectButton.addActionListener(this);
    }

    private void placeComponents() {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(5, 5, 5, 5);

        // Username
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        add(usernameFieldLabel, gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        add(usernameField, gbc);

        // Host
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        add(hostFieldLabel, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        add(hostField, gbc);

        // ConnectButton
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        add(connectButton, gbc);
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        if (ae.getSource().equals(connectButton)) {
            if (usernameField.getText().isEmpty()) {
                JOptionPane.showMessageDialog(this, "Bitte geben sie einen Usernamen an.", "Usernamen angeben",
                        JOptionPane.INFORMATION_MESSAGE);
            }
            else if (hostField.getText().isEmpty()) {
                JOptionPane.showMessageDialog(this, "Bitte geben sie einen Host an.", "Host angeben",
                        JOptionPane.INFORMATION_MESSAGE);
            }
            else {
                controller.connectToServer(hostField.getText(), usernameField.getText());
            }
        }
    }
}
